﻿using Common;
using Library_CommonData;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SQLServerHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Library.Controllers
{
    public class HomeController : Controller
    {

        SqlHelper dbo = new SqlHelper();
        List<Syscolumns> columnsList = new List<Syscolumns>();
        string sqlStr = "";

        /// <summary>
        /// 列表页
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public ActionResult Index()
        {

            return View();
        }

        public string MethodList(string tableName)
        {

            string columnsStr = "*";
            columnsStr = CacheData.GetTableColumnsNameStr(tableName);

            DataTable dt = dbo.SqlExecuteDataTable("select " + columnsStr + " from " + tableName + "");
            string result = JsonConvert.SerializeObject(dt, new DataTableConverter());
            return result;
        }


        /// <summary>
        /// 添加方法视图
        /// </summary>
        /// <returns></returns>
        public ActionResult AddMethod()
        {

            return View();
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="tb_Method"></param>
        /// <returns></returns>
        public int Add_Method(tb_Method tb_Method)
        {


            tb_Method.languageID = CacheData.GetLanguageIdByName(tb_Method.languageID).ToString();
            SqlParameter[] sqlParam = ModelToSql.GetParameter<tb_Method>(tb_Method, ref sqlStr, tb_Method.tableName, "id");
            return dbo.SqlExecuteNoneQuery(sqlStr, sqlParam);
        }

        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="tb_Method"></param>
        /// <returns></returns>
        public int Update_Method(tb_Method tb_Method)
        {
            SqlParameter[] sqlParam = ModelToSql.GetUpdateParameter<tb_Method>(tb_Method, ref sqlStr, tb_Method.tableName, "id");
            return dbo.SqlExecuteNoneQuery(sqlStr, sqlParam);
        }


        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="tb_Method"></param>
        /// <returns></returns>
        public int Delete_Method(tb_Method tb_Method)
        {
            SqlParameter[] parame = new SqlParameter[] { new SqlParameter("@id", tb_Method.id) };
            return dbo.SqlExecuteNoneQuery("delete " + tb_Method.tableName + " where id=@id", parame);
        }

    }
}